package com.zhkj.txg.module.cart.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.chad.library.adapter.base.entity.node.BaseNode
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.http.BaseResponse
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.HttpResponseException
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.lib.utils.RxBus
import com.zhkj.txg.module.cart.entity.CartNumberChangeResponse
import com.zhkj.txg.module.cart.entity.CartResponse
import com.zhkj.txg.module.cart.model.CartManager
import com.zhkj.txg.module.home.entity.HotGoodsResponse
import com.zhkj.txg.module.home.ui.BackTabEvent

@Suppress("UNCHECKED_CAST")
class CartViewModel(application: Application) : BaseViewModel(application) {
    private val cartManager = CartManager()
    val cartLiveData = MutableLiveData<HttpResponse<CartResponse>>()
    val changeNumberLiveData = MutableLiveData<HttpResponse<CartNumberChangeResponse>>()

    //    val hotGoodsLiveData = MutableLiveData<HttpResponse<HotGoodsResponse>>()
    val deleteCartLiveData = MutableLiveData<HttpResponse<BaseResponse>>()

    private val numb = 20
    var page = 1
        private set

    fun getCart(
        token: String,
        restart: Boolean = true
    ) {
        val listener = object : ViewModelSubscribeListener<CartResponse>(cartLiveData) {
            override fun onSuccess(resp: CartResponse) {
                if (resp.data != null) for (shop in resp.data.cartList) {
                    shop.childNode = shop.goodsList as? MutableList<BaseNode>
                }
                super.onSuccess(resp)
                if (restart) getProducts(token, true)
            }

            override fun onFailure(throwable: Throwable) {
                if (throwable is HttpResponseException && throwable.code == 401) RxBus.post(
                    BackTabEvent()
                )
                super.onFailure(throwable)
            }
        }
        val cart = cartManager.getCart(token, listener)
        dispose.add(cart)
    }

    fun getCartNew(map: Map<String, String>) {
        val listener = object : ViewModelSubscribeListener<CartResponse>(cartLiveData) {
            override fun onSuccess(resp: CartResponse) {
                super.onSuccess(resp)
            }

            override fun onFailure(throwable: Throwable) {
                if (throwable is HttpResponseException && throwable.code == 401) RxBus.post(
                    BackTabEvent()
                )
                super.onFailure(throwable)
            }
        }
        val cart = cartManager.getCartNew(map, listener)
        dispose.add(cart)
    }

    fun getProducts(token: String, restart: Boolean = false) {
//        val listener = object : ViewModelSubscribeListener<HotGoodsResponse>(hotGoodsLiveData) {
//            override fun onSuccess(resp: HotGoodsResponse) {
//                resp.hasMore = resp.data.size >= numb
//                resp.loadMore = page > 1
//                super.onSuccess(resp)
//                page += 1
//            }
//        }
//        if (restart) page = 1
//        val hotGoods = cartManager.hotGoods(token, page, numb, listener)
//        dispose.add(hotGoods)
    }

    fun changeCartNumber(token: String, cartId: Long, number: Int) {
        changeCartNumber(cartId, number)
    }

    fun changeCartNumber(cartId: Long, number: Int) {
        val listener =
            object : ViewModelSubscribeListener<CartNumberChangeResponse>(changeNumberLiveData) {
                override fun onSuccess(resp: CartNumberChangeResponse) {
                    resp.cartId = cartId
                    resp.currentNumber = number
                    super.onSuccess(resp)
                }
            }
        val map = hashMapOf("cart_id" to cartId.toString(), "goods_num" to number.toString())
        val changeCartNumber = cartManager.changeCartNumber(map, listener)
        dispose.add(changeCartNumber)
    }

    fun deleteCart(
        token: String,
        cartIds: String
    ) {
        val listener = ViewModelSubscribeListener(deleteCartLiveData)
        val deleteCart = cartManager.deleteCart(token, cartIds, listener)
        dispose.add(deleteCart)
    }

    fun deleteCart(
        token: String,
        cartIds: MutableList<String>
    ) {
        val listener = ViewModelSubscribeListener(deleteCartLiveData)
        val deleteCart = cartManager.deleteCart(token, cartIds, listener)
        dispose.add(deleteCart)
    }
}